package com.example.rocketmq.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.rocketmq.dao.OrderDao;
import com.example.rocketmq.pojo.Order;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RocketMQMessageListener(topic = "tp_springboot_01", consumerGroup = "consumer_grp_03")
public class MyRocketListener implements RocketMQListener<String> {
    @Autowired
    OrderDao orderDao;
    @Override
    public void onMessage(String message) {
        // 处理broker推送过来的消息
        log.info("我收到创建订单消息"+message);
        JSONObject jsonObject=JSON.parseObject(message);
        int num = jsonObject.getIntValue("num");
        String order_id = jsonObject.getString("order_id");
        if(num>0){//判断是否是超卖订单
            //订单 入库
            Order order=new  Order();
            order.setOrder_no(order_id);
            order.setO_type("0");
            orderDao.save(order);
        }else{
            System.out.println("商品卖完了，下次再来");
        }

    }
}
